package cc.rengu.igas.channel.upac.core.service.trans;


import cc.rengu.igas.channel.upac.common.constant.UpacTreeNodeConstant;
import cc.rengu.igas.channel.upac.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.upac.common.enums.UpacProdEnum;
import cc.rengu.igas.channel.upac.core.model.ChnlBizResponse;
import cc.rengu.igas.channel.upac.core.service.base.CreditPayService;
import cc.rengu.igas.channel.upac.facade.request.RepayRequest;
import cc.rengu.igas.channel.upac.facade.response.RepayResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.entity.DstChannelInfo;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;

/**
 * 银联全渠道单笔代付服务 on 2019/10/8.
 */
public class SingleRepayService extends CreditPayService {

    @Override
    protected Object beforeBizProcess() throws Exception {
        RepayRequest repayRequest = new RepayRequest();
        ConvertUtil.convertOutput(repayRequest);
        return repayRequest;
    }

    @Override
    protected ChnlBizResponse callBizService(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        int iReturnCode = 0;
        ChnlBizResponse chnlBizResponse = new ChnlBizResponse();
        RepayResponse repayResponse = new RepayResponse();
        RepayRequest repayRequest = (RepayRequest) request;
        String acctEncFlag = xmlTreeUtil.getXmlTreeStringValue(UpacTreeNodeConstant.ACCT_ENC_FLAG);
        String payAcctNo = repayRequest.getPayAcctNo();
        if (AppParamConstant.YES.equals(acctEncFlag)) {
            repayRequest.setPayAcctNo(encryptData(payAcctNo));
        }
        DstChannelInfo dstChannelInfo = (DstChannelInfo) xmlTreeUtil.getXmlTreeObjectValue(UpacTreeNodeConstant.DST_CHANNEL_INFO, DstChannelInfo.class);
        ConvertUtil.convertInput(repayRequest);
        if (null != repayRequest.getRiskCtrlInfo()) {
            packageRiskCtrlInfo(repayRequest.getRiskCtrlInfo());
        }
        /* 付款方信息处理 */
        if (null != repayRequest.getPayerInfo()) {
            packagePayerInfo(repayRequest.getPayerInfo());
        }

        /* 持卡人身份信息处理 */
        if (null != repayRequest.getCustomerInfo()) {
            packageCustomerInfo(repayRequest.getCustomerInfo(), null);
        }
        xmlTreeUtil.setXmlTreeStringValue(UpacTreeNodeConstant.txnType, "12");
        xmlTreeUtil.setXmlTreeStringValue(UpacTreeNodeConstant.txnSubType, "00");
        xmlTreeUtil.setXmlTreeStringValue(UpacTreeNodeConstant.bizType, UpacProdEnum.PAYMENT.getProdCode());
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TXN_NUM, repayRequest.getHeader().getTxnNum());
        iReturnCode = schedule.callService(dstChannelInfo.getDstServiceName(), 0);
        if (98 == iReturnCode) {
            rglog.error("调用银联全渠道服务<{}>超时，iReturnCode:<{}>", dstChannelInfo.getDstServiceName(), iReturnCode);
            repayResponse.setRespCode(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode());
            repayResponse.setRespDesc(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespDesc());
            chnlBizResponse.setResult(repayResponse);
            return chnlBizResponse;
        } else if (0 != iReturnCode) {
            rglog.error("调用银联全渠道服务<{}>失败，iReturnCode:<{}>", dstChannelInfo.getDstServiceName(), iReturnCode);
            throw new BizException(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
        }
        ConvertUtil.convertOutput(repayResponse);
        repayResponse.setRespCode(convertRespCode(repayResponse.getRespCode()));

        chnlBizResponse.setResult(repayResponse);
        return chnlBizResponse;
    }

    @Override
    protected void afterBizProcess(ChnlBizResponse response) throws Exception {
        RepayResponse repayResponse = (RepayResponse) response.getResult();
        ConvertUtil.convertInput(repayResponse);
    }

}
